package uk.co.agileworks.bidpirate.web;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

import uk.co.agileworks.bidpirate.model.Bid;

@Component
public class BidValidator implements Validator<Bid> {
	
	public final boolean validate(Bid bid, Errors errors) {
		
		if (bid.getItem().getEndTime().isBeforeNow()) {
			errors.reject("item.ended");
		}
		if (bid.getMaxBid() != null && bid.getMaxBid() < bid.getItem().getPrice()) {
			errors.reject("maxBid.too.low");
		}
		return !errors.hasErrors();
	}

}
